<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流水单号" prop="serialNum">
        <el-input
          v-model="queryParams.serialNum"
          placeholder="请输入流水单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="项目名称" prop="settleProjectName">
        <el-input
          v-model="queryParams.settleProjectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>





      <el-form-item label="对方单位" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入对方单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>



      <el-form-item label="收件人" prop="receiver">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入收件人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="applyUserName">
        <el-input
          v-model="queryParams.applyUserName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:drawBill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:drawBill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:drawBill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:drawBill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drawBillList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水单号" align="center" prop="serialNum" />
      <el-table-column label="开票日期" align="center" prop="billDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算编号" align="center" prop="settleNum" />
      <el-table-column label="项目名称" align="center" prop="settleProjectName" />
      <el-table-column label="开票总金额" align="center" prop="billTotalAmount" />
      <el-table-column label="可开票金额" align="center" prop="ableBillAmount" />
      <el-table-column label="结算金额" align="center" prop="settleTotalAmount" />
      <el-table-column label="我方开票公司" align="center" prop="billCompany" />
      <el-table-column label="发票类型" align="center" prop="billType" />
      <el-table-column label="税率" align="center" prop="taxRate" />
      <el-table-column label="对方单位" align="center" prop="company" />
      <el-table-column label="地址及电话" align="center" prop="adressPhone" />
      <el-table-column label="开户行、账户" align="center" prop="openBankAccount" />
      <el-table-column label="开票信息附件" align="center" prop="billAttached" />
      <el-table-column label="发票栏备注" align="center" prop="billNote" />
      <el-table-column label="收件人" align="center" prop="receiver" />
      <el-table-column label="联系方式" align="center" prop="receiverPhone" />
      <el-table-column label="收件地址" align="center" prop="receiverAddress" />
      <el-table-column label="收件信息附件" align="center" prop="receiverAttached" />
      <el-table-column label="申请人id" align="center" prop="applyUserId" />
      <el-table-column label="申请人" align="center" prop="applyUserName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:drawBill:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:drawBill:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改开票申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px"  append-to-body>
      <el-form ref="form"  inline :model="form" :rules="rules"  label-width="100px">

        <el-form-item label="开票日期" prop="billDate">
          <el-date-picker clearable
            v-model="form.billDate"
            type="date"

            value-format="yyyy-MM-dd"
            placeholder="请选择开票日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算编号" prop="settleNum">
          <el-input v-model="form.settleNum" placeholder="请输入结算编号" >
            <template slot="append">
              <el-button style="width:60px" type="primary" @click="openfn">选择</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="settleProjectName">
          <el-input v-model="form.settleProjectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="开票总金额" prop="billTotalAmount">
          <el-input v-model="form.billTotalAmount" placeholder="请输入开票总金额" />
        </el-form-item>
        <el-form-item label="可开票金额" prop="ableBillAmount">
          <el-input v-model="form.ableBillAmount" placeholder="请输入可开票金额" />
        </el-form-item>
        <el-form-item label="结算金额" prop="settleTotalAmount">
          <el-input v-model="form.settleTotalAmount" placeholder="请输入结算金额" />
        </el-form-item>
        <el-form-item label="我方开票公司" prop="billCompany">
          <el-input v-model="form.billCompany" placeholder="请输入我方开票公司" />
        </el-form-item>
        <el-form-item label="税率" prop="taxRate">
          <el-input v-model="form.taxRate" placeholder="请输入税率" />
        </el-form-item>
        <el-form-item label="对方单位" prop="company">
          <el-input v-model="form.company" placeholder="请输入对方单位" />
        </el-form-item>
        <el-form-item label="地址及电话" prop="adressPhone">
          <el-input v-model="form.adressPhone" placeholder="请输入地址及电话" />
        </el-form-item>
        <el-form-item label="开户行、账户" prop="openBankAccount">
          <el-input v-model="form.openBankAccount" placeholder="请输入开户行、账户" />
        </el-form-item>
        <el-form-item label="开票信息附件" prop="billAttached">
          <el-input v-model="form.billAttached" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="发票栏备注" prop="billNote">
          <el-input v-model="form.billNote" placeholder="请输入发票栏备注" />
        </el-form-item>
        <el-form-item label="收件人" prop="receiver">
          <el-input v-model="form.receiver" placeholder="请输入收件人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="receiverPhone">
          <el-input v-model="form.receiverPhone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="收件地址" prop="receiverAddress">
          <el-input v-model="form.receiverAddress" placeholder="请输入收件地址" />
        </el-form-item>
        <el-form-item label="收件信息附件" prop="receiverAttached">
          <el-input v-model="form.receiverAttached" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请人" prop="applyUserName">
          <el-input v-model="form.applyUserName" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="可选结算单"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose">
      <el-table :data="statementList" style="width: 100%">
        <el-table-column align="center" prop="number" label="结算单编号" width="180"></el-table-column>
        <el-table-column align="center"  prop="customerName" label="客户名称" width="180"></el-table-column>
        <el-table-column align="center"  prop="projectName" label="项目名称"></el-table-column>
        <el-table-column align="center"  prop="payAmount" label="应收合计"></el-table-column>
        <el-table-column align="center"  label="操作">
          <template slot-scope="scope">
            <el-button @click="checked(scope.row)" type="primary" size="mini">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>

  </span>
    </el-dialog>

  </div>
</template>

<script>
import { listDrawBill, getDrawBill, delDrawBill, addDrawBill, updateDrawBill,getstatement,getmoney} from "@/api/module/drawBill";
import {getStatementListApi} from "@/api/module/statemente"
export default {
  name: "DrawBill",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 开票申请表格数据
      drawBillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNum: null,
        billDate: null,
        settleNum: null,
        settleProjectName: null,
        billTotalAmount: null,
        ableBillAmount: null,
        settleTotalAmount: null,
        billCompany: null,
        billType: null,
        taxRate: null,
        company: null,
        adressPhone: null,
        openBankAccount: null,
        billAttached: null,
        billNote: null,
        receiver: null,
        receiverPhone: null,
        receiverAddress: null,
        receiverAttached: null,
        applyUserId: null,
        applyUserName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      statementList:[],
      dialogVisible:false
    };
  },

  created() {
    this.getList();
    this.form.applyUserId=this.$store.getters.id
    this.form.applyUserId=thgis.$store.getters.name
  },
  methods: {
    //获取可开票对账单
    getstatementList(){

      getstatement({}).then(res=>{
        this.statementList=res.data

      })
    },
    //打开弹框
    openfn(){
      this.dialogVisible=true
    },
    //点击空白区域关闭
    handleClose(){
      this.dialogVisible=false
    },
    //选中
    checked(item){
      this.dialogVisible=false
      this.form.settleNum=item.number
      this.form.settleTotalAmount=item.payAmount
      this.form.settleProjectName=item.projectName
      getmoney(this.form.settleNum).then(res=>{
        this.form.ableBillAmount=res.data
      })

    },
    /** 查询开票申请列表 */
    getList() {
      this.loading = true;
      listDrawBill(this.queryParams).then(response => {
        this.drawBillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNum: null,
        billDate: null,
        settleNum: null,
        settleProjectName: null,
        billTotalAmount: null,
        ableBillAmount: null,
        settleTotalAmount: null,
        billCompany: null,
        billType: null,
        taxRate: null,
        company: null,
        adressPhone: null,
        openBankAccount: null,
        billAttached: null,
        billNote: null,
        receiver: null,
        receiverPhone: null,
        receiverAddress: null,
        receiverAttached: null,
        applyUserId: null,
        applyUserName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加开票申请";
      this.getstatementList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDrawBill(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改开票申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDrawBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrawBill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除开票申请编号为"' + ids + '"的数据项？').then(function() {
        return delDrawBill(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/drawBill/export', {
        ...this.queryParams
      }, `drawBill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
